package com.github.wui.circle;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.GradientDrawable;
import android.text.Layout;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.EditText;

import com.github.wui.R;

/**
 * @author <a href="https://github.com/karai-oss">Mr.xie</a>
 * @Date 2025/4/15
 */
public class CircleEditText extends androidx.appcompat.widget.AppCompatEditText {


    private int bgColor = Color.WHITE;

    private float round = 10;
    private Paint paint;

    /**
     * 边框宽度
     */
    private float border_thin = 1;

    private int border_color = Color.BLACK;

    public CircleEditText(Context context) {
        super(context);

    }


    public CircleEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CircleEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);

    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleEditText);
        bgColor = typedArray.getColor(R.styleable.CircleEditText_circle_edit_text_background_color, Color.WHITE);
        round = typedArray.getDimension(R.styleable.CircleEditText_circle_edit_text_round, 10f);
        border_thin = typedArray.getDimension(R.styleable.CircleEditText_circle_edit_border_thin, 2);
        border_color = typedArray.getColor(R.styleable.CircleEditText_circle_edit_text_border_color, Color.BLACK);
        typedArray.recycle();

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = (int) (MeasureSpec.getSize(heightMeasureSpec));

        setMeasuredDimension(width, height );

    }



    @Override
    protected void onDraw(Canvas canvas) {

        canvas.translate(0,getHeight() / 30);
        // 绘制文本
        super.onDraw(canvas);

        // 绘制背景
        GradientDrawable background =new GradientDrawable();
        background.setCornerRadius(round);
        background.setColor(bgColor);
        background.setStroke((int) border_thin, border_color);
        setBackgroundDrawable(background);


    }


    public int getBgColor() {
        return bgColor;
    }

    public void setBgColor(int bgColor) {
        this.bgColor = bgColor;
        setBackground(null);
        invalidate();
    }

    public float getRound() {
        return round;
    }

    public void setRound(float round) {
        this.round = round;
        setBackground(null);
        invalidate();
    }


    public float getBorder_thin() {
        return border_thin;
    }

    public void setBorder_thin(float border_thin) {
        this.border_thin = border_thin;
        setBackground(null);
        invalidate();
    }

    public int getBorder_color() {
        return border_color;
    }

    public void setBorder_color(int border_color) {
        this.border_color = border_color;
        setBackground(null);
        invalidate();
    }


}
